package com.yjx.gulimall.product.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.yjx.gulimall.common.valid.AddGroup;
import com.yjx.gulimall.common.valid.UpdateGroup;
import com.yjx.gulimall.common.vo.product.BrandVO;
import com.yjx.gulimall.common.vo.product.CategoryBrandVO;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.yjx.gulimall.product.entity.CategoryBrandRelationEntity;
import com.yjx.gulimall.product.service.CategoryBrandRelationService;
import com.yjx.gulimall.common.utils.PageUtils;
import com.yjx.gulimall.common.utils.R;



/**
 * 品牌分类关联
 *
 * @author zhuyingcoder
 * @email 1210736981@qq.com
 * @date 2022-01-23 12:30:17
 */
@RestController
@RequestMapping("product/categorybrandrelation")
public class CategoryBrandRelationController {
    @Autowired
    private CategoryBrandRelationService categoryBrandRelationService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("product:categorybrandrelation:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = categoryBrandRelationService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("product:categorybrandrelation:info")
    public R info(@PathVariable("id") Long id){
		CategoryBrandRelationEntity categoryBrandRelation = categoryBrandRelationService.getById(id);

        return R.ok().put("categoryBrandRelation", categoryBrandRelation);
    }

    /**
     * 保存 关联详细信息
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:categorybrandrelation:save")
    public R save(@Validated(AddGroup.class) @RequestBody CategoryBrandRelationEntity categoryBrandRelation){

        System.out.println(categoryBrandRelation);
		categoryBrandRelationService.saveDetail(categoryBrandRelation);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:categorybrandrelation:update")
    public R update(@Validated(UpdateGroup.class) @RequestBody CategoryBrandRelationEntity categoryBrandRelation){
		categoryBrandRelationService.updateById(categoryBrandRelation);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:categorybrandrelation:delete")
    public R delete(@RequestBody Long[] ids){
		categoryBrandRelationService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/deleteById")
    //@RequiresPermissions("product:categorybrandrelation:delete")
    public R deleteById(@RequestBody Long id){

        categoryBrandRelationService.removeById(id);

        return R.ok();
    }

    @GetMapping("/catelog/list")
    public R categoryBrandList(@RequestParam(value = "brandId",required = true) Long brandId){

        List<CategoryBrandVO> categoryBrandVOList = categoryBrandRelationService.getCategoryBrandList(brandId);
        return R.ok().put("data",categoryBrandVOList);
    }

    @GetMapping("/brands/list")
    public R getBrandList(@RequestParam Map<String, Object> params){

        List<BrandVO> brandVOList = new ArrayList<>();
        String catId = (String) params.get("catId");
        if(!StringUtils.isEmpty(catId)){
            brandVOList = categoryBrandRelationService.getBrandListByCatId(Long.parseLong(catId));
        }
        return R.ok().put("data",brandVOList);
    }

}
